import { ref } from 'vue'
import { defineStore } from 'pinia'
import { addDays } from '@/utils/string-helper'
import { EARN_MEDIA_FILTER } from '@/constant'
import API from '@/http'

export const useEarnStore = defineStore('earn', () => {
  /**
   * @description 是否顯示收益頁面，用於觸發收益頁面的API
   */
  const isTrigger = ref(false)
  const startDate = ref(addDays(-7))
  const endDate = ref(new Date())

  const filters = ref([
    { label: 'common.all', type: 'all', value: EARN_MEDIA_FILTER.ALL },
    { label: 'common.hotTop5', type: 'topfive', value: EARN_MEDIA_FILTER.TOP5_VIEW },
    { label: 'info.media.video', type: 'video', value: EARN_MEDIA_FILTER.VIDEO },
    { label: 'info.media.photo', type: 'photo', value: EARN_MEDIA_FILTER.PHOTO },
    { label: 'common.store', type: 'shop', value: EARN_MEDIA_FILTER.SHOP },
  ])
  const selectedFilter = ref(filters.value[0])

  const dialog = ref(false)

  // 收益頁面的餘額
  const balance = ref(0)

  // 刷新餘額
  async function refreshBalance() {
    try {
      const response = await API.Earn.balance({ data: {} })
      balance.value = response.balance
    } catch (e) {
      console.error(e)
    }
  }

  function turnOn() {
    isTrigger.value = true
  }

  function turnOff() {
    isTrigger.value = false
  }

  function open() {
    dialog.value = true
  }

  function close() {
    dialog.value = false
  }

  const tabSwiping = ref(false)

  return {
    isTrigger,
    dialog,
    selectedFilter,
    filters,
    tabSwiping,
    startDate,
    endDate,
    balance,
    turnOn,
    turnOff,
    open,
    close,
    refreshBalance,
  }
})
